<?php
/*汇款计划*/
header("Content-Type: text/html;charset=UTF-8");
class Payment_record extends CI_Controller {
	function __construct() {
		parent::__construct ();
		$this->load->helper ( 'url' );
		$this->load->library ( 'pagination' );
	}

	public function index($offset = 0) {
		$user_name=$this->session->userdata('username');
		if($user_name){
			$this->load->model ( 'payment_record_model', 'recordmodel' );
			$limit = 10;
			$total = $this->recordmodel->count_nums ();
			$data['total']=$total;
			$data['offset']=$offset;
			$data ['payment_records'] = $this->recordmodel->list_record_plans ( $limit, $offset );
			$current_total=count($data ['payment_records']);
			$data['current_total']=$offset+$current_total;
			
			$config ['base_url'] = base_url () . 'index.php/payment_record/index/';
			$config ['total_rows'] = $total;
			$config ['per_page'] = $limit;
			$config ['num_links'] = 4;
			$config ['first_link'] = '首页';
			$config ['last_link'] = '尾页';
			$this->pagination->initialize ( $config );
			$data ['pag_links'] = $this->pagination->create_links ();
			
			$data ['page_title'] = '回款记录 — 昊美科技CRM系统';
			$data ['class4'] = 'cur';
			$data ['all_users'] = $this->get_all_users();
			$this->load->view ( 'payment_record_list', $data );
		}else{
			$this->load->view ( 'login');
		}
	}

    public function check($name = ''){ /*搜索*/
		$user_id = $this->session->userdata('id');
		$role = $this->session->userdata('role');
		$this->db->select('contract.subject as contract_subject,contract.contract_id as contract_id,customer.customer_name as customer_name,payment_record.*,user.user_name as user_name')->from('payment_record')->join('contract', 'payment_record.contract_id = contract.contract_id')->join('customer', 'payment_record.customer_id = customer.id')->join('user','payment_record.owner_id=user.id');
		if($role == '市场'){
			$this->db->where('payment_record.owner_id',$user_id);
		}
		$this->db->like('payment_record.contract_name',urldecode($name));
		$query = $this->db->get();
		$data['payment_records'] = $query->result();
		$data ['page_title'] = '回款记录 — 昊美科技CRM系统';
		$data ['class4'] = 'cur';
		$this->load->view ( 'payment_record_list', $data );
	}

	/**
	 * 数据所有者查询
	 * */
	public function check_user_messages($id = ''){
		$data['payment_records'] = $this->list_record_plans($id);
		$data ['pag_links'] = '';
		$data ['page_title'] = '回款记录 — 昊美科技CRM系统';
		$data ['class4'] = 'cur';
		$data ['all_users'] = $this->get_all_users();
		$query = $this->db->query('SELECT user_name FROM user where id = '.$id.'');
		$row = $query->row();
		$data ['check_user_name'] = $row->user_name;
		$this->load->view ( 'payment_record_list', $data );
	}
	
	public function find($id = '') { /*查看*/
		$user_id = $this->session->userdata('id');
		$role = $this->session->userdata('role');
		$this->db->select('contract.subject as contract_subject,contract.contract_id as contract_id,customer.customer_name as customer_name,payment_record.*,user.user_name as user_name')->from('payment_record')->join('contract', 'payment_record.contract_id = contract.contract_id')->join('customer', 'payment_record.customer_id = customer.id')->join('user','payment_record.owner_id=user.id');
		if($role == '市场'){
			$this->db->where('payment_record.owner_id',$user_id);
		}
		$this->db->where('payment_record.id',$id);
		$query = $this->db->get();
		$data['payment_record'] = $query->row();
		$this->load->view ( 'payment_record_view', $data );
	}

	public function edit($id = ''){
		$user_id = $this->session->userdata('id');
		$role = $this->session->userdata('role');
		$this->db->select('contract.subject as contract_subject,contract.contract_id as contract_id,customer.customer_name as customer_name,payment_record.*,user.user_name as user_name')->from('payment_record')->join('contract', 'payment_record.contract_id = contract.contract_id')->join('customer', 'payment_record.customer_id = customer.id')->join('user','payment_record.owner_id=user.id');
		if($role == '市场'){
			$this->db->where('payment_record.owner_id',$user_id);
		}
		$this->db->where('payment_record.id',$id);
		$query = $this->db->get();
		$data['payment_record'] = $query->row();

		/*获取所有用户*/
		$this->db->select('*')->from('user');
		$query = $this->db->get();
		$data['users'] = $query->result();

		$this->load->view ( 'payment_record_edit',$data);
	}

	public function update(){
		$data_array = $_POST;
		/*获取对应客户编号*/
		$this->db->select ( 'id' )->from ( 'customer' )->where ( 'customer_name', $data_array ['customer_name'] );
		$query = $this->db->get ();
		$row = $query->row();
		$customer_id = $row->id;
		$data_array ['customer_id'] = $customer_id;

		$now_time = date ( 'Y-m-d H:i:s', strtotime ( 'now' ) );
		$data_array['update_time'] = $now_time;

		$id=$data_array ['record_id'];
		unset($data_array ['record_id']);
		unset($data_array ['customer_name']);

		$this->db->where('id', $id);
		$row=$this->db->update('payment_record', $data_array);
		
		/*更新合同表数据*/
		$contract_id=$data_array['contract_id'];
		$query = $this->db->query("SELECT money FROM payment_record where contract_id = $contract_id");
		$total_money = 0;
		foreach ($query->result() as $row){
			$total_money+=$row->money;
		}
		
		$query = $this->db->query("SELECT total_amount FROM contract where contract_id = $contract_id");
		$row = $query->row();
		if($total_money != $row->total_amount){
			$contract_array =array('payment_amount' => $total_money,'status' => '执行中');
			$data_plan = array(
					'back' => '0',
			);
		}else{
			$query = $this->db->query("SELECT stages FROM payment_plan where contract_id = $contract_id");
			$row = $query->row();
			$total_stages = $row->stages; /*总其次*/
			
			$query = $this->db->query("SELECT count(*) as nums FROM payment_record where contract_id = $contract_id");
			$row = $query->row();
			$real_stages = $row->nums; /*实际还款其次*/
			
			if($total_stages != $real_stages){
				$contract_array =array('payment_amount' => $total_money,'status' => '执行中');
				$data_plan = array(
						'back' => '0',
				);
			}else{
				$contract_array =array('payment_amount' => $total_money,'status' => '完成');
				$data_plan = array(
						'back' => '1',
				);
			}
		}
		$this->db->where('contract_id', $contract_id);
		$row=$this->db->update('contract', $contract_array);
		
		$this->db->where('contract_id', $contract_id); /*回款计划状态*/
		$this->db->update('payment_plan', $data_plan);
		redirect ( 'payment_record/index/' );
	}

	public function show_Payment_record_add(){
		/*获取所有用户*/
		$this->db->select('*')->from('user');
		$query = $this->db->get();
		$data['users'] = $query->result();
		$this->load->view ( 'payment_record_add',$data);
	}

	public function insert(){
		$data_array = $_POST;

		/*获取对应客户编号*/
		$this->db->select ( 'id' )->from ( 'customer' )->where ( 'customer_name', $data_array ['customer_name'] );
		$query = $this->db->get ();
		$row = $query->row();
		$customer_id = $row->id;
		$data_array ['customer_id'] = $customer_id;

		$now_time = date ( 'Y-m-d H:i:s', strtotime ( 'now' ) );
		$data_array['create_time'] = $now_time;

		/*获取对应合同*/
		$this->db->select ( 'subject' )->from ( 'contract' )->where ( 'contract_id', $data_array ['contract_id'] );
		$query = $this->db->get ();
		$row = $query->row();
		$subject = $row->subject;
		$data_array ['contract_name'] = $subject;
		unset($data_array ['customer_name']);
		
		$q=$this->db->insert('payment_record', $data_array);
		if($q > 0){
			$contract_id = $data_array ['contract_id'];
			$query = $this->db->query("SELECT money FROM payment_record where contract_id = $contract_id");
			$payment_money=0;
			foreach ($query->result() as $row){
				$payment_money += $row->money;
			}
			
			$query = $this->db->query("SELECT total_amount FROM contract where contract_id = $contract_id");
			$row = $query->row();
			$total_amount = $row->total_amount;
			if($payment_money != $total_amount){
				$data = array(
						'payment_amount' => $payment_money,
						'status' => '执行中',
				);
				$data_plan = array(
						'back' => '0',
				);
			}else{
				$query = $this->db->query("SELECT stages FROM payment_plan where contract_id = $contract_id");
				$row = $query->row();
				$total_stages = $row->stages; /*总其次*/
				
				$query = $this->db->query("SELECT count(*) as nums FROM payment_record where contract_id = $contract_id");
				$row = $query->row();
				$real_stages = $row->nums; /*实际还款其次*/
				
				if($total_stages != $real_stages){
					$data = array(
							'payment_amount' => $payment_money,
							'status' => '执行中',
					);
					$data_plan = array(
							'back' => '0',
					);
				}else{
					$data = array(
							'payment_amount' => $payment_money,
							'status' => '完成',
					);
					$data_plan = array(
							'back' => '1',
					);
				}
			}
			
			$this->db->where('contract_id', $contract_id); /*合同表状态*/
			$this->db->update('contract', $data); 
			
			$this->db->where('contract_id', $contract_id); /*回款计划状态*/
			$this->db->update('payment_plan', $data_plan);
		}
		redirect ( 'payment_record/index/' );
	}


	public function delete($id = ''){
		$query = $this->db->query("SELECT contract_id FROM payment_record where id = $id");
		$row = $query->row();
		$data = array(
				'back' => 0,
		);
		$contract_id = $row->contract_id;
		$row = $this->db->delete ( 'payment_record', array ('id' => $id ) );
		$this->db->where('contract_id', $contract_id);
		$q=$this->db->update('payment_plan', $data);
		
		if($q > 0){
			$query = $this->db->query("SELECT money FROM payment_record where contract_id = $contract_id");
			$payment_money=0;
			foreach ($query->result() as $row){
				$payment_money += $row->money;
			}
			$data = array(
					'payment_amount' => $payment_money,
					'status' => '执行中',
			);
			$this->db->where('contract_id', $contract_id);
			$n = $this->db->update('contract', $data);
		}
		
		redirect ( 'payment_record/index/' );
	}
	
	/*获取回款计划期次*/
	public function get_plan_stages(){
		$data_array = $_POST;
		
		$plan_id = $data_array['plan_id'];
		$this->db->select ( 'stages' )->from ( 'payment_plan' )->where ( 'contract_id', $plan_id );
		$query = $this->db->get ();
		$row = $query->row();
		$stages = $row->stages;
		
		$query = $this->db->query("SELECT count(id) as nums FROM payment_record where contract_id = $plan_id"); /*获取已付的数量*/
		$row = $query->row();
		$record = $row->nums;
		
		$nums = $stages-$record; //剩余的数量
		$nums_array = array('stages' =>$stages,'nums' =>$nums);
		$json_string = json_encode($nums_array);
		echo $json_string;
	}
	
	/*检测金额*/
	public function check_payment_money(){
		$data_array = $_POST;
		$contract_id = $data_array['contarct_id'];
		$payment_money = $data_array['money'];
		$query = $this->db->query("SELECT total_amount FROM contract where contract_id = $contract_id");
		$row = $query->row();
		$total_amount = $row->total_amount;/*合同总金额*/
		
		$query = $this->db->query("SELECT money FROM payment_record where contract_id = $contract_id");
		$total_money =0; /*已汇款金额*/
		foreach ($query->result() as $row){
			$total_money+=$row->money;
		}
		$record_id = $data_array['already_id'];
		$query = $this->db->query("SELECT money FROM payment_record where id = $record_id"); 
		$row = $query->row();
		$already = $row->money;/*本次回款*/
		
		$already_money = $total_money-$already;
		$max_money = $already_money+$payment_money;
		if($max_money > $total_amount){ /*回款金额总和大于总合同额*/
			echo 1;
		}else{
			echo '';
		}
	}
	
	public function check_add_record_money(){
		$data_array = $_POST;
		$contract_id = $data_array['contarct_id'];
		$input_money = $data_array['input_money'];
		
		$query = $this->db->query("SELECT total_amount FROM contract where contract_id = $contract_id");
		$row = $query->row();
		$total_amount = $row->total_amount;/*合同总金额*/
		
		$query = $this->db->query("SELECT money FROM payment_record where contract_id = $contract_id");
		$already_total_money =0; /*已汇款金额*/
		foreach ($query->result() as $row){
			$already_total_money+=$row->money;
		}
		
		$money = $input_money+$already_total_money;
		if($money > $total_amount){ /*回款金额总和大于总合同额*/
			echo 1;
		}else{
			echo '';
		}
	}
	
	/*获取所有用户*/
	public function get_all_users(){
		$query = $this->db->query("SELECT * FROM user order by create_time asc");
		return $query->result();
	}
	
	public function list_record_plans($user_id = 0){ 
		if($user_id == 0){
			redirect ( 'payment_record/index/' );
		}else{
			$this->db->select('contract.subject as contract_subject,contract.contract_id as contract_id,customer.customer_name as customer_name,payment_record.*,user.user_name as user_name')->from('payment_record')->join('contract', 'payment_record.contract_id = contract.contract_id')->join('customer', 'payment_record.customer_id = customer.id')->join('user','payment_record.owner_id=user.id');
			$this->db->where('payment_record.owner_id',$user_id);
			$this->db->order_by('payment_record.create_time', "desc");
			$query = $this->db->get();
			return $query->result();
		}
	}
}
?>
